package com.xjwlmq.dx.servlet;

import com.alibaba.fastjson.JSON;
import com.xjwlmq.dx.dao.RainQualityBaseDao;
import com.xjwlmq.dx.dao.impl.RainQualityImpl;
import com.xjwlmq.dx.entity.RainQuality;
import com.xjwlmq.dx.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @ClassName RainQualityListServlet
 * @Description TODO:
 * @Author 12994
 * @Date 2024/9/30 16:46
 * @Version 1.0
 **/
@WebServlet("/getList")
public class RainQualityListServlet extends HttpServlet {
    RainQualityImpl rainQuality = new RainQualityImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.service(req, resp);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html:charset=UTF-8");

        if (req.getParameter("id") != null) {
            // 获得单个对象
            // TODO: 其实应该返回一个长度为1的List, 前端拿到之后判断长度为1之后继续进行解析
            RainQuality rainQuality1 = rainQuality.getRainQualityViaId(Integer.parseInt(req.getParameter("id")));
            resp.getWriter().write(JSON.toJSONString(rainQuality1));
        } else {
            // 获得用户列表
            ArrayList<RainQuality> rainQualities = rainQuality.getRainQualityList();
            String stustr = JSON.toJSONString(rainQualities);
            resp.getWriter().write(stustr);
        }


    }
}
